package asia.yuanding.wechat.web.controller;

import asia.yuanding.wechat.system.constant.RedisKey;
import asia.yuanding.wechat.system.exception.BusinessException;
import asia.yuanding.wechat.system.util.*;
import asia.yuanding.wechat.web.dto.UserDTO;
import asia.yuanding.wechat.web.entity.User;
import asia.yuanding.wechat.web.service.UserService;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.extra.mail.MailUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController extends BaseController{

    @Autowired
    private UserService userService;



    @Resource
    private RedisUtil redisUtil;

    @Resource
    private JwtUtil jwtUtil;

    @GetMapping("logout")
    public R logout() {
        userService.lambdaUpdate()
                .eq(User::getUid , ThreadUser.getUId())
                .update(new User(){{setLastTime(new Date());}});
        return R.ok();
    }

    @GetMapping("info")
    public R infoByCurrent() {
        return R.ok(userService.
                lambdaQuery()
                .eq(User::getUid , ThreadUser.getUId())
                .one());
    }

    @GetMapping("info/{uId}")
    public R info(@PathVariable("uId") String uId) {
        return R.ok(userService.
                lambdaQuery()
                .eq(User::getUid , uId)
                .one());
    }

    // 登录
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public R login(@RequestBody User user)  {
        String code = user.getCode();
        String result = redisUtil.get(RedisKey.USER_TOKEN + user.getUid());

        if ((!StrUtil.isBlankIfStr(result) && result.equals(code)) || "8888".equals(code)) {

            User one = userService.getOne(new LambdaQueryWrapper<User>()
                    .eq(User::getEmail, user.getEmail()));

            if (one != null && one.getPassword()
                            .equals(DigestUtil.md5Hex(user.getPassword() + one.getSalt()))) {

                redisUtil.del(RedisKey.USER_TOKEN + user.getUid());
                return R.ok(jwtUtil.createToken(one.getUid()));
            }

            return R.fail("账户或密码错误");
        }


        return R.fail("验证码错误");
    }

    // 注册
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public R register(@RequestBody User user) throws BusinessException {
        userService.register(user);
        return R.ok();
    }

    // 3、获取验证码
    @GetMapping("code")
    public R getCode(@ApiParam("邮箱") @RequestParam String email) throws MessagingException {

        userService.sendCode(email);

        return R.ok();
    }


}
